package com.yh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.javabean.Permission;
import com.yh.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "page",required = false,defaultValue = "1") int page,
                          @RequestParam(value = "limit",required = false,defaultValue = "5")int limit,
                          HttpServletRequest request){
        PageHelper.startPage(page, limit);
        //调用service层查询数据方法
        List<Permission> all = permissionService.findAll();
        PageInfo<Permission> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo",pageInfo);
        return "/permission/permission-list";
    }

    @RequestMapping("/add")
    public String add(Permission permission){
        permissionService.addPermission(permission);
        return "redirect:findAll";
    }

    @RequestMapping("/deleteById")
    public String deleteById(int id){
        permissionService.delete(id);
        return "redirect:findAll";
    }

    @RequestMapping("/edit")
    public String edit(Permission permission){
        permissionService.editPermission(permission);
        return "redirect:findAll";
    }

    @RequestMapping("/findById")
    public String findById(int id,HttpServletRequest request){
        Permission permission = permissionService.findById(id);
        request.setAttribute("permission",permission);
        return "/permission/permission-edit";
    }







}
